package cn.com.dhc.danlu.util;

import android.annotation.SuppressLint;
import android.os.CountDownTimer;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.com.dhc.danlu.R;

/**
 * 手机验证码计时器
 */
public class VerificationCodeTimerUtil extends CountDownTimer {
    public static final int IN_RUNNING = 1001;
    public static int END_RUNNING = 1002;
    private LinearLayout sendSMS;// 按钮
    private TextView sendSMSText;//

    // 在这个构造方法里需要传入三个参数，一个是Activity，一个是总的时间millisInFuture，一个是countDownInterval，然后就是你在哪个按钮上做这个是，就把这个按钮传过来就可以了
    public VerificationCodeTimerUtil (long millisInFuture, long countDownInterval, LinearLayout sendSMS, TextView sendSMSText) {
        super (millisInFuture, countDownInterval);
        this.sendSMS = sendSMS;
        this.sendSMSText = sendSMSText;
    }

    /**
     * @param millisInFuture
     *         倒计时的时长
     * @param countDownInterval
     *         间隔时间
     */
    public VerificationCodeTimerUtil (long millisInFuture, long countDownInterval) {
        super (millisInFuture, countDownInterval);
    }

    @SuppressLint ("SetTextI18n")
    @Override
    public void onTick (long millisUntilFinished) {
        // 设置不能点击
        sendSMS.setClickable (false);
        // 设置倒计时时间
        sendSMSText.setText (millisUntilFinished / 1000 + "秒后重新获取");
        // 设置按钮为灰色，这时是不能点击的 umeng setBackGround();
        sendSMS.setBackgroundResource (R.drawable.rounded_gray_text_bg);
    }

    @Override
    public void onFinish () {
        sendSMSText.setText (R.string.register_verify_code);
        // 重新获得点击
        sendSMS.setClickable (true);
        sendSMS.setBackgroundResource (R.drawable.rounded_text_bg);
    }
}